#include "ZBuffer.h"
#include "ZLog.h"

namespace ZuiLib {

	ZBuffer::ZBuffer(size_t size) 
		:m_data((uint8_t*)zmalloc(size+1)), m_size(size)
	{
		LOG_IF(!m_data) << "malloc error size=" << size;
		if (m_data) m_data[size] = '\0';
	}

	ZBuffer::ZBuffer(const uint8_t* data, size_t size) 
		: m_data((uint8_t*)zmalloc(size+1)), m_size(size)
	{
		LOG_IF(!m_data) << "malloc error size=" << size;

		if (m_data) {
			memcpy(m_data, data, size);
			m_data[size] = '\0';
		}
	}

	ZBuffer::~ZBuffer() {
		zfree(m_data);
	}

	ZRefCountedPtr<ZBuffer> ZBuffer::Create(const uint8_t* data, size_t size) {
		return new ZRefCountedObject<ZBuffer>(data, size);
	}

	ZRefCountedPtr<ZBuffer> ZBuffer::Create(size_t size) {
		return new ZRefCountedObject<ZBuffer>(size);
	}

}//namesapce
